package com.cjh.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cjh.annotation.WebLog;
import com.cjh.pojo.Admin;
import com.cjh.pojo.Goods;
import com.cjh.pojo.Order;
import com.cjh.pojo.User;
import com.cjh.service.OrderService;
import com.cjh.vo.ResultMap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orderApi")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/insertOrder")
    @WebLog(description = "请求=>添加商品订单接口")
    public ResultMap insertOrder(@RequestParam(value = "goodsId",required = false) Integer goodsId,
                                 HttpSession session){
        User user = (User) session.getAttribute("user");
        //设置订单信息
        Order order=new Order();
        order.setUser(new User().setId(user.getId()));
        order.setGoods(new Goods().setId(goodsId));
        order.setDate(new Date());
        //保存订单
        orderService.insertOrder(order);
        return ResultMap.ok(200,"添加商品订单","已订购",1);
    }

    @GetMapping("/userOrderList")
    @WebLog(description = "请求=>用户订单信息接口")
    public ResultMap userOrderList(
                                @RequestParam(value = "page", defaultValue = "1",required = false) Integer page,
                                @RequestParam(value = "limit", defaultValue = "10",required = false) Integer limit,
                                HttpSession session) {

        User user = (User) session.getAttribute("user");
        PageHelper.startPage(page, limit);
        List<Order> orders = orderService.selectOrdersByUserId(user.getId());
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return ResultMap.ok(0, "用户订单信息->每页10条数据", pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @DeleteMapping("/deleteOrder")
    @WebLog(description = "请求=>删除用户订单接口")
    public ResultMap deleteOrder(@RequestParam(value = "id",required = false) Integer id) {

        orderService.removeById(id);
        return ResultMap.ok(200, "删除用户订单", "删除成功",1);
    }

    /**
     * 未处理的订单
     * @param page
     * @param limit
     * @param order 搜索实体类
     * @param session
     * @return
     */
    @GetMapping("/untreatedOrderList")
    @WebLog(description = "请求=>所有未派送的订单信息接口")
    public ResultMap untreatedOrderList(
            @RequestParam(value = "page", defaultValue = "1",required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10",required = false) Integer limit,
            Order order, HttpSession session) {
        System.out.println(order);
        PageHelper.startPage(page, limit);
        List<Order> orders = orderService.selectUntreatedOrders(order);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return ResultMap.ok(0, "所有未派送的订单信息->每页10条数据", pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @PutMapping("/updateOrderState")
    @WebLog(description = "请求=>处理用户订单接口")
    public ResultMap updateOrderState(@RequestParam(value = "id",required = false) Integer id,
                                    HttpSession session) {

        Admin admin = (Admin) session.getAttribute("admin");
        //update t_order set aid=1,state=1 where id=26
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //将订单改成已派送状态
        updateWrapper.set("state", 1);
        //设置管理员
        updateWrapper.set("admin_id", admin.getId());
        updateWrapper.eq("id", id);
        orderService.update(updateWrapper);
        return ResultMap.ok(200, "处理用户订单", "发货成功",1);

    }

    @GetMapping("/getOrderNumber")
    @WebLog(description = "请求=>查询用户订单数量接口")
    public ResultMap getOrderNumber(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer orderNumber = orderService.getOrderNumber(user.getId());

        return ResultMap.ok(200,"查询用户订单数量",orderNumber);
    }

}
